//
//  NSDateAdditions.m
//  Tint
//
//  Created by Kevin Wu on 2/22/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "NSDateAdditions.h"


@implementation NSDate (Tint)

#pragma mark - Date components

- (NSDateComponents *)dateComponents
{
    NSCalendarUnit calendarUnit = 
    NSEraCalendarUnit | 
    NSYearCalendarUnit | 
    NSMonthCalendarUnit | 
    NSDayCalendarUnit |
    NSHourCalendarUnit | 
    NSMinuteCalendarUnit | 
    NSSecondCalendarUnit | 
    NSWeekCalendarUnit | 
    NSWeekdayCalendarUnit | 
    NSWeekdayOrdinalCalendarUnit | 
    NSQuarterCalendarUnit | 
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_5_0
    NSWeekOfMonthCalendarUnit | 
    NSWeekOfYearCalendarUnit | 
    NSYearForWeekOfYearCalendarUnit | 
#endif
    NSCalendarCalendarUnit | 
    NSTimeZoneCalendarUnit;
    return [[NSCalendar currentCalendar] components:calendarUnit fromDate:self]; // AUTO
}



#pragma mark - Adjusting

- (NSDate *)startDateOfDay
{
	NSDateComponents *components = [self dateComponents];
	[components setHour:0];
	[components setMinute:0];
	[components setSecond:0];
	return [[NSCalendar currentCalendar] dateFromComponents:components]; // AUTO
}

- (NSDate *)noonDateOfDay
{
    NSDateComponents *components = [self dateComponents];
	[components setHour:12];
	[components setMinute:0];
	[components setSecond:0];
	return [[NSCalendar currentCalendar] dateFromComponents:components]; // AUTO
}



- (NSDate *)newDateByAddingSeconds:(NSInteger)seconds
{
    NSTimeInterval timeInterval = [self timeIntervalSinceReferenceDate] + seconds;
    return [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:timeInterval];
}

- (NSDate *)newDateByAddingMinutes:(NSInteger)minutes
{
	NSTimeInterval timeInterval = [self timeIntervalSinceReferenceDate] + minutes * (60.0);
	return [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:timeInterval];
}

- (NSDate *)newDateByAddingHours:(NSInteger)hours
{
	NSTimeInterval timeInterval = [self timeIntervalSinceReferenceDate] + hours * (60.0*60.0);
	return [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:timeInterval];
}

- (NSDate *)newDateByAddingDays:(NSInteger)days
{
	NSTimeInterval timeInterval = [self timeIntervalSinceReferenceDate] + days * (60.0*60.0*24);
	return [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:timeInterval];
}




#pragma mark - Comparing

- (BOOL)isEarlierThanDate:(NSDate *)date
{
	return ( [self earlierDate:date] == self );
}


- (BOOL)isSameYearAsDate:(NSDate *)date
{
    NSDateComponents *components1 = [self dateComponents];
    NSDateComponents *components2 = [date dateComponents];
	return ( [components1 year] == [components2 year] );
}

- (BOOL)isSameMonthAsDate:(NSDate *)date
{
    NSDateComponents *components1 = [self dateComponents];
    NSDateComponents *components2 = [date dateComponents];
	return (([components1 year] == [components2 year]) && 
			([components1 month] == [components2 month])
            );
}

- (BOOL)isSameDayAsDate:(NSDate *)date
{
    NSDateComponents *components1 = [self dateComponents];
    NSDateComponents *components2 = [date dateComponents];
	return (([components1 year] == [components2 year]) && 
			([components1 month] == [components2 month]) && 
			([components1 day] == [components2 day])
            );
}

// This hard codes the assumption that a week is 7 days
- (BOOL)isSameWeekAsDate:(NSDate *)date
{
    NSDateComponents *components1 = [self dateComponents];
    NSDateComponents *components2 = [date dateComponents];
	
	// Must be same week. 12/31 and 1/1 will both be week "1" 
    // if they are in the same week
	if ( [components1 week] != [components2 week] ) {
        return NO;
    }
	
	// Must have a time interval under 1 week. Thanks @aclark
	return ( abs([self timeIntervalSinceDate:date]) < (60.0*60.0*24*7) );
}

@end
